package com.htkj.demo.dialog;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.htkj.demo.CacheUtil;
import com.htkj.demo.R;
import com.htkj.demo.adapter.MySpinnerAdapter;
import com.htkj.demo.bean.SpinnerData;
import com.lxj.xpopup.core.CenterPopupView;

import java.util.ArrayList;
import java.util.List;

public class SettingDialog extends CenterPopupView {


    public SettingDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        List<SpinnerData> spinnerDataList=new ArrayList<>();
        spinnerDataList.add(new SpinnerData(0L,"系统内核"));
        spinnerDataList.add(new SpinnerData(1L,"EXO内核"));
        spinnerDataList.add(new SpinnerData(2L,"IJk内核"));
        int kernel=CacheUtil.INSTANCE.getInt("kernel",0);
        Spinner mSpinner =findViewById(R.id.spinner_list);
        SpinnerAdapter mSpinnerAdapter=new MySpinnerAdapter(getContext(),spinnerDataList);
        mSpinner.setAdapter(mSpinnerAdapter);
        mSpinner.setSelection(kernel);

        int hardware=CacheUtil.INSTANCE.getInt("hardware",0);
        CheckBox checkBox =findViewById(R.id.cb_hardware);
        checkBox.setChecked(hardware==1);

        String ip=CacheUtil.INSTANCE.getString("IP","192.168.0.202");
        EditText mIp =findViewById(R.id.et_ip);
        mIp.setText(ip);

        String MAC=CacheUtil.INSTANCE.getString("MAC","00:00:00:00:01");
        EditText mMac =findViewById(R.id.et_mac);
        mMac.setText(MAC);


        findViewById(R.id.btn_sver).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                SpinnerData mSpinnerData  = (SpinnerData) mSpinner.getSelectedItem();
                int id= Math.toIntExact(mSpinnerData.getId());
                CacheUtil.INSTANCE.setInt("kernel", id);

                CacheUtil.INSTANCE.setInt("hardware",checkBox.isChecked()?1:0);
                String ip=mIp.getText().toString();
                if(!TextUtils.isEmpty(ip)){
                    CacheUtil.INSTANCE.setString("IP",ip);
                }
                String mac=mMac.getText().toString();
                if(!TextUtils.isEmpty(mac)){
                    CacheUtil.INSTANCE.setString("MAC",mac);
                }
                dismiss();
                Toast.makeText(getContext(),"保存成功",0).show();
            }
        });


    }

    // 返回自定义弹窗的布局
    @Override
    protected int getImplLayoutId() {
        return R.layout.dialog_setting;
    }

}
